package com.czl.module_storehouse.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_storehouse.R;

import java.util.List;

/**
 * @author xubin
 */
public class AllocatePostOutAdapter extends BaseQuickAdapter<SortBean, BaseViewHolder> {

    public AllocatePostOutAdapter(int layoutResId, @Nullable List<SortBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, SortBean item) {
        helper.setText(R.id.tv_title, item.getSortName())
                .setText(R.id.tv_model, "型号：" + (item.getSortModel() == null ? "" : item.getSortModel()))
                .setText(R.id.tv_num, "数量：" + item.applyNum())
                .setText(R.id.tv_allocate_num, String.valueOf(item.getAllocateCount()))
                .setVisible(R.id.iv_right, getAllocateNum(item) > 0)
                .setText(R.id.tv_allocated_num, "已调：" + item.lendNum());
        helper.getView(R.id.cl_root)
                .setOnClickListener(v -> {
                    if (getAllocateNum(item) == 0) {
                        return;
                    }
                    if (getOnItemChildClickListener() != null) {
                        getOnItemChildClickListener().onItemChildClick(AllocatePostOutAdapter.this, v, helper.getLayoutPosition());
                    }
                });
    }

    /**
     * 待调拨数量
     *
     * @return
     */
    public int getAllocateNum(SortBean sortBean) {
        if (sortBean.getApplyNum() == null) {
            return 0;
        }
        if (sortBean.getLendNum() == null) {
            return sortBean.getApplyNum();
        }
        return sortBean.applyNum() - sortBean.lendNum();
    }
}
